package com.faithnmn.learnenglish.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.faithnmn.learnenglish.util.AnimationUtils;
import com.faithnmn.learnenglish.util.Resources;
import com.faithnmn.learnenglish.util.Utils;


public class CustomProgressDialog extends ProgressDialog {

	private TextView messageTextView;
	private ImageView loading;
	private String msg;
	public boolean inited = false;
	private Handler handler = new Handler();
	private LinearLayout layout;
	public CustomProgressDialog(Context context, int theme) {
		super(context, theme);
	}
	
	public CustomProgressDialog(int margin20dp,Context context) {
		super(context);
		layout = new LinearLayout(context);
		layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		layout.setBackgroundColor(Color.WHITE);
		loading = new ImageView(context);
		BitmapDrawable loadingBackground = new BitmapDrawable(context.getResources(),Utils.decodeBitmapFromBase64(Resources.loadingPrgress,margin20dp * 5 / 2,margin20dp * 5 / 2));
		loading.setImageDrawable(loadingBackground);
		loading.setScaleType(ScaleType.FIT_XY);
		loading.startAnimation(AnimationUtils.getRotationAnimation());
		messageTextView = new TextView(context);
		messageTextView.setText(msg == null?"Loading":msg);
		messageTextView.setTextSize(14);
		messageTextView.setTextColor(Color.BLACK);
		LinearLayout.LayoutParams progressLayoutParam = new LinearLayout.LayoutParams(margin20dp * 3 / 2,margin20dp * 3 / 2);
		LinearLayout.LayoutParams messageLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		progressLayoutParam.gravity = Gravity.CENTER_VERTICAL;
		messageLayoutParam.gravity = Gravity.CENTER_VERTICAL;
		progressLayoutParam.leftMargin = margin20dp;
		progressLayoutParam.rightMargin = margin20dp;
		progressLayoutParam.bottomMargin = margin20dp / 2;
		progressLayoutParam.topMargin = margin20dp / 2;
		messageLayoutParam.rightMargin = margin20dp;
		messageLayoutParam.bottomMargin = margin20dp / 2;
		messageLayoutParam.topMargin = margin20dp / 2;
		layout.setOrientation(LinearLayout.HORIZONTAL);
		layout.addView(loading, progressLayoutParam);
		layout.addView(messageTextView, messageLayoutParam);
		layout.setGravity(Gravity.CENTER);
		inited = true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(layout);
	}
	
	@Override
	public void setMessage(CharSequence message) {
		if(messageTextView != null){
			messageTextView.setText(message);
			loading.requestLayout();
			messageTextView.requestLayout();
			loading.invalidate();
			messageTextView.invalidate();
		}else{
			msg = (String) message;
		}
	}
	
	private Runnable doShow = new Runnable() {
		@Override
		public void run() {
			if(inited){
				show();
			}else{
				handler.postDelayed(this, 100);
			}
		}
	};
	
	@Override
	public void dismiss() {
		loading.clearAnimation();
		handler.removeCallbacks(doShow);
		super.dismiss();
	};
	
	@Override
	public void show() {
		if(inited){
			super.show();
			loading.startAnimation(AnimationUtils.getRotationAnimation());
		}else{
			handler.postDelayed(doShow, 100);
		}
	}
	
}
